package icu.bughub.app.app.ui.screens

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import icu.bughub.app.app.R
import icu.bughub.app.app.compositionLocal.LocalUserViewModel
import kotlinx.coroutines.launch

@SuppressLint("UnusedBoxWithConstraintsScope")
@Composable
fun LoginScreen(back: () -> Unit = {}) {

    /* //屏幕宽度
     val screenWidth: Float
     val screenHeight: Float

     with(LocalDensity.current) {
         screenWidth = LocalConfiguration.current.screenWidthDp.dp.toPx()
         screenHeight = LocalConfiguration.current.screenHeightDp.dp.toPx()
     }*/

    val userViewModel = LocalUserViewModel.current

    var userName by remember {
        mutableStateOf("")
    }


    var passWord by remember {
        mutableStateOf("")
    }

    var showPassword by remember {
        mutableStateOf(false)
    }


    val coroutineScope = rememberCoroutineScope()


    BoxWithConstraints(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        Image(
            painter = painterResource(id = R.drawable.bg),
            contentDescription = "Background",
            modifier = Modifier
                .fillMaxSize(),
            contentScale = ContentScale.Crop
        )

        //右上往左下渐变
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    Brush.linearGradient(
                        colors = listOf(
                            Color(0xffbb8378),
                            Color.Transparent
                        ),
                        //start = Offset(x = screenWidth, y = 0f),
                        start = Offset(x = constraints.maxWidth.toFloat(), y = 0f),
                        // end = Offset(x = 0f, y = screenHeight)
                        end = Offset(x = 0f, y = constraints.maxHeight.toFloat())
                    )
                )
        )

        //左下往右上渐变
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    Brush.linearGradient(
                        colors = listOf(
                            Color(0xff149ee7),
                            Color.Transparent
                        ),
                        /*start = Offset(x = 0f, y = screenHeight),
                        end = Offset(x = screenWidth, y = 0f)*/
                        start = Offset(x = 0f, y = constraints.maxHeight.toFloat()),
                        end = Offset(x = constraints.maxWidth.toFloat(), y = 0f)

                    )
                )
        )
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(8.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.SpaceBetween
        ) {

            IconButton(
                onClick = {
                    back.invoke()
                }, modifier = Modifier
                    .align(Alignment.Start)
                    .padding(top = 10.dp)
            ) {
                Icon(
                    imageVector = Icons.Default.Close,
                    contentDescription = "返回", tint = Color.White
                )
            }

            Column(
                modifier = Modifier
                    .weight(3f),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text = "用户登录",
                    color = Color.White,
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center,
                )
            }

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
                    .weight(3f),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                TextField(
                    value = userName, onValueChange = {
                        userName = it
                    }, singleLine = true, leadingIcon = {
                        Icon(
                            imageVector = Icons.Default.Person,
                            contentDescription = "用户名",
                            tint = Color.White
                        )
                    }, label = {
                        Text(text = "用户名", fontSize = 14.sp, color = Color.White)
                    }, colors = TextFieldDefaults.colors(
                        focusedContainerColor = Color.Transparent,
                        unfocusedContainerColor = Color.Transparent,
                        focusedIndicatorColor = Color.LightGray,
                        unfocusedIndicatorColor = Color.LightGray,
                        focusedLabelColor = Color.LightGray,
                        unfocusedLabelColor = Color.LightGray,
                        cursorColor = Color.White,
                        focusedTextColor = Color.White,
                        unfocusedTextColor = Color.White
                    )
                )
                TextField(
                    trailingIcon = {
                        IconButton(onClick = {
                            showPassword = !showPassword
                        }) {
                            Icon(
                                imageVector = if (showPassword) Icons.Default.Visibility else Icons.Default.VisibilityOff,
                                contentDescription = "密码",
                                tint = Color.White
                            )
                        }
                    },
                    value = passWord,
                    onValueChange = {
                        passWord = it
                    },
                    singleLine = true,
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Default.Lock,
                            contentDescription = "密码",
                            tint = Color.White
                        )
                    },
                    visualTransformation = if (!showPassword) PasswordVisualTransformation() else VisualTransformation.None,
                    label = {
                        Text(text = "密码", fontSize = 14.sp, color = Color.White)
                    },
                    colors = TextFieldDefaults.colors(
                        focusedContainerColor = Color.Transparent,
                        unfocusedContainerColor = Color.Transparent,
                        focusedIndicatorColor = Color.LightGray,
                        unfocusedIndicatorColor = Color.LightGray,
                        focusedLabelColor = Color.LightGray,
                        unfocusedLabelColor = Color.LightGray,
                        cursorColor = Color.White
                    )
                )

                Spacer(
                    modifier = Modifier
                        .height(16.dp)
                )

                TextButton(onClick = {
                    coroutineScope.launch {
                        userViewModel.login {
                            back.invoke()
                        }
                    }
                }) {
                    Text(text = "登录 ", color = Color.White, fontSize = 16.sp)
                }
            }

            TextButton(onClick = {}) {
                Text(
                    text = "还没有账号？点击立即注册", color = Color.LightGray,
                    fontSize = 12.sp
                )
            }
        }


    }
 

}

@Preview
@Composable
fun LoginScreenPreview() {
    LoginScreen()
}